package com.sky.controller.user;

import com.sky.constant.CommonConstant;
import com.sky.dto.OrdersDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.entity.OrderDetail;
import com.sky.exception.ShoppingCartBusinessException;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("OrderController")
@RequestMapping("/user/order")
@Slf4j
@Api(tags = "C端-订单接口")
public class OrderContorller {
    @Autowired
    OrderService orderService;

    @Autowired
    @Qualifier("redisTemplate")
    private RedisTemplate template;

    @PostMapping("/submit")
    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO){
        Integer status =(Integer) template.opsForValue().get(CommonConstant.SHOP_STATUS);
        if(status==0 || status==null){
            throw new ShoppingCartBusinessException("店铺已经打烊了");
        }
        OrderSubmitVO orderSubmitVO= orderService.submit(ordersSubmitDTO);
        return Result.success(orderSubmitVO);
    }
    @PutMapping("/payment")
    public Result<OrderPaymentVO> pay(@RequestBody OrdersPaymentDTO ordersPaymentDTO){
        OrderPaymentVO orderPaymentVO= orderService.pay(ordersPaymentDTO);
        return Result.success(orderPaymentVO);
    }
    @GetMapping("/paysuccess")
    public Result paysuccess(@RequestParam Long orderId){
        orderService.paySuccess(orderId);
        return Result.success();
    }
    @GetMapping("/payerror")
    public Result payerror(@RequestParam Long orderId){
        orderService.payError(orderId);
        return Result.success();
    }
    @GetMapping("/orderDetail/{id}")
    public Result<OrderVO> orderDetail(@PathVariable Long id){
        OrderVO orderVO=orderService.orderDetail(id);
        return Result.success(orderVO);
    }
    @GetMapping("/historyOrders")
    public Result<PageResult> historyOrder(OrdersPageQueryDTO ordersPageQueryDTO){
        PageResult page=orderService.historyOrder(ordersPageQueryDTO);
        return Result.success(page);

    }
    @PutMapping("/cancel/{id}")
    public Result cancel(@PathVariable Long id){
        orderService.cancel(id);
        return Result.success();
    }
    @GetMapping("/reminder/{id}")
    public Result reminder(@PathVariable Long id){
        orderService.reminder(id);
        return Result.success();
    }
}
